package com.car.service.Impl;

import com.car.entity.Coupon;
import com.car.entity.Exchangetable;
import com.car.entity.GrabThe;
import com.car.entity.Integral;
import com.car.mapper.CouponMapper;
import com.car.mapper.ExchangetableMapper;
import com.car.mapper.GrabTheMapper;
import com.car.mapper.IntegralMapper;
import com.car.mapper.UserMapper;
import com.car.service.ExchangetableService;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
@Service
public class ExchangetableServiceImpl implements ExchangetableService{

	@Autowired
	ExchangetableMapper exchangetableMapper;
	
	@Autowired
	UserMapper userMapper;
	
	@Autowired	
	CouponMapper couponMapper;
	
	@Autowired	
	GrabTheMapper grabTheMapper;
	
	@Autowired	
	IntegralMapper integralMapper;
	
	@Override
	public int deleteByPrimaryKey(Integer changeId) {
		// TODO Auto-generated method stub
		return 0;
	}

	

	@Override
	public Exchangetable selectByPrimaryKey(Integer changeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Exchangetable> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByPrimaryKey(Exchangetable record) {
		// TODO Auto-generated method stub
		return 0;
	}



	@Override
	@Transactional
	public int insert(Exchangetable record, Integer integral, Coupon coupon, GrabThe grabThe, Integral integral2) {
		// TODO Auto-generated method stub
		int nums;
		try {
			nums = exchangetableMapper.insert(record);
			if(nums>0){
				nums = userMapper.updvipScore(integral, record.getUserId());
				if(nums>0){
					coupon.setTheNameofTheCoupon(grabThe.getCarRental());
					coupon.setAmountOfCoupon(Double.valueOf(grabThe.getTheVolumeof()));
					Calendar c = Calendar.getInstance();
					c.setTime(new Date());
					c.add(Calendar.MONTH, 3);
					coupon.setEndTime(c.getTime());
					coupon.setRulesOfUse(grabThe.getUseRules());
					coupon.setUserId(record.getUserId());
					coupon.setGrabId(grabThe.getGrabId());
					nums = couponMapper.insert(coupon);
					if(nums>0){
						integral2.setIntegralNum(grabThe.getTheIntegral());
						integral2.setIntegrationUseDate(new Date());
						integral2.setUserId(record.getUserId());
						integral2.setIntegrationUseType(2); //2为减
						nums = integralMapper.insertToIntrgral(integral2);
					}else{
						throw new RuntimeException("添加积分详情失败！");
					}
				}else{
					throw new RuntimeException("添加优惠卷失败！");
				}
			}else{
				throw new RuntimeException("添加积分使用记录失败！");
			}
			return nums;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		
		}
		
		return 0;
	}



	@Override
	public List<Exchangetable> selectByuserId(Integer userId) {
		// TODO Auto-generated method stub
		return exchangetableMapper.selectByuserId(userId);
	}
}